package filecopier.gui.scripteditor;

import java.io.File;
import java.util.Collection;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import custom.swing.FileListPanel;
import filecopier.options.Options;
import filecopier.script.Script;
import filecopier.script.CopyScript;

public class ScriptListPanel extends FileListPanel {

    private Options.CopyScriptType copyScriptType = null;
    private javax.swing.JDialog dialog = null;

    public ScriptListPanel(final Collection<? extends File> c, final FileFilter fileFilter, int selectionMode) {
        super(filecopier.Singleton.getInstance().getFrame(), c, fileFilter, selectionMode);
    }

    public ScriptListPanel(final Collection<? extends File> c, final FileFilter fileFilter, int selectionMode, boolean multiEnabled) {
        super(filecopier.Singleton.getInstance().getFrame(), c, fileFilter, selectionMode, multiEnabled);
    }

    public ScriptListPanel(final javax.swing.JDialog dialog, final Collection<? extends File> c, final Options.CopyScriptType copyScriptType) {
        super(filecopier.Singleton.getInstance().getFrame(), c, getFileFilter(copyScriptType), FILES_ONLY);

        this.copyScriptType = copyScriptType;
        this.dialog = dialog;
    }

    private static FileFilter getFileFilter(final Options.CopyScriptType copyScriptType) {
        FileFilter fileFilter = null;
        String ext = null;
        if (copyScriptType == Options.CopyScriptType.From) {
            ext = "fsc";
        } else if (copyScriptType == Options.CopyScriptType.To) {
            ext = "tsc";
        }

        fileFilter = new FileNameExtensionFilter("Copy Script Files", "csc", ext);
        return fileFilter;
    }

    @Override
    protected void addListMouseListener() {
        if (list != null) {
            list.addMouseListener(new java.awt.event.MouseAdapter() {

                @Override
                public void mouseClicked(java.awt.event.MouseEvent e) {
                    int index = list.locationToIndex(e.getPoint());
                    if (index == getPrevIndex()) {
                        int clickCount = e.getClickCount();
                        if (clickCount == 1) {
                            showEditFld(index);
                        } else if (clickCount == 2) {
                            // Hide the edit field in case it is visible
                            hideEditFld();

                            // Open the script selected
                            File scriptFile = (File) list.getSelectedValue();

                            try {
                                Script script = new CopyScript(scriptFile);
                                new ScriptEditorDialog(copyScriptType, script, null, false);
                            } catch (custom.xml.DOMHelperException dhe) {
                                javax.swing.JOptionPane.showMessageDialog(dialog, "\"" + scriptFile + "\" is in an invalid format.",
                                        "Invalid Script File", javax.swing.JOptionPane.ERROR_MESSAGE);

                                filecopier.Singleton.getInstance().getFileLogger().error("DOMHelperException encountered", dhe);
                            } catch (java.io.IOException ioe) {
                                javax.swing.JOptionPane.showMessageDialog(dialog, "\"" + scriptFile + "\" is in an invalid format.",
                                        "Invalid Script File", javax.swing.JOptionPane.ERROR_MESSAGE);

                                filecopier.Singleton.getInstance().getFileLogger().error("IOException encountered", ioe);
                            }
                        }
                    } else {
                        hideEditFld();
                        setPrevIndex(index);
                    }
                }
            });
        }
    }
}
